package org.dolphin.action.uom;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dolphin.base.bean.MeasurementVO;
import org.dolphin.base.constant.Measurement;
import org.dolphin.base.json.PaginationBean;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custom-default")
@Namespace(value = "/uom")
@InterceptorRefs(value = {@InterceptorRef(value = "customStack")})
public class UomAction extends ActionSupport {
  private static final long serialVersionUID = -8015691914774086149L;
  private PaginationBean pagination;

  public PaginationBean getPagination() {
    return pagination;
  }

  @Action(value = "getUoms", results = @Result(name = "success", params = {"root", "pagination"}, type = "json"))
  public String listUoms() {
    Measurement[] measurements = Measurement.values();
    List<MeasurementVO> list = new ArrayList<MeasurementVO>(measurements.length);
    for (Measurement measurement : measurements) {
      MeasurementVO vo = new MeasurementVO();
      vo.setKey(measurement.name());
      vo.setValue(getText(measurement.getResourceKey()));
      list.add(vo);
    }
    pagination = new PaginationBean();
    pagination.setRows(list);
    return SUCCESS;
  }

  public void setPagination(PaginationBean pagination) {
    this.pagination = pagination;
  }
}
